package baidumap

import (
	"openapi.itspire.cn/utils"
	"io/ioutil"
	"net/http"
)

func GetIpLookup(ip string) (string , string, string) {
	ak := "qqqq"
	url := "https://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll"
	res, err := http.Get(url)
	if err == nil {
		defer res.Body.Close()
		body, err := ioutil.ReadAll(res.Body)
		if err != nil {
			panic(err)
		}

		addrMap := utils.JsonDecode(string(body))
		content := addrMap["content"]
		addressDetail := content.(map[string]interface{})["address_detail"]
		country := "中国"
		province := addressDetail.(map[string]interface{})["province"]
		city := addressDetail.(map[string]interface{})["city"]
		return country, province.(string), city.(string)
	}
	return "境外", "", ""
}